{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Making 2d plot animation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as ani\n", "import IPython\n", "%matplotlib agg\n", "\n", "# import plons scripts\n", "import plons\n", "import plons.SmoothingKernelScript as sk\n", "import plons.PhysicalQuantities as pq\n", "import plons.ConversionFactors_cgs as cgs\n", "import plons.Plotting as plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting information about data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "prefix = \"wind\"\n", "loc = \"/STER/matse/Papers/Esseldeurs+2023/Phantom/High/binary6Free/\"\n", "outputloc = \".\"\n", "dump = loc+\"wind_00600\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading setup and dump" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "setup = plons.LoadSetup(loc, prefix)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "dumpData = plons.LoadFullDump(dump, setup)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Animation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I will create an animation with a 2D slice plot on the left, and a line slice on the right." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1, figsize=(7, 7))\n", "ax.set_aspect('equal')\n", "ax.set_facecolor('k')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating 2D slice plot on the left" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "n = 500\n", "x = np.linspace(-30*cgs.au, 30*cgs.au, n)\n", "y = np.linspace(-30*cgs.au, 30*cgs.au, n)\n", "X, Y = np.meshgrid(x, y)\n", "Z = np.zeros_like(X)\n", "smooth_rot = sk.smoothMesh(X, Y, Z, dumpData, ['rho'])\n", "\n", "mesh = ax.pcolormesh(X/cgs.au, Y/cgs.au, np.log10(smooth_rot[\"rho\"]+1e-99), cmap=plt.cm.get_cmap('inferno'), vmin=-17, vmax = -14)\n", "ax.set_xlim(x[0]/cgs.au, x[-1]/cgs.au)\n", "ax.set_ylim(y[0]/cgs.au, y[-1]/cgs.au)\n", "\n", "circleAGB, circleComp = plot.plotSink(ax, dumpData, setup)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Defining what to update in each frame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each frame, an angle theta will be returned. Create a line at this angle, calculate the smoothed values, and plot them on the figures" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def animate(frame):\n", " dump = loc+prefix+f\"_%05i\"%frame\n", " dumpData = plons.LoadFullDump(dump, setup)\n", " smooth_rot = sk.smoothMesh(X, Y, Z, dumpData, ['rho'])\n", "\n", " mesh.set_array(np.log10(smooth_rot[\"rho\"]+1e-99))\n", " circleAGB.center = dumpData['posAGB']/cgs.au\n", " circleComp.center = dumpData['posComp']/cgs.au" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating the animation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By creating the animation, the frames will represent the angles from the previous function. Interval represents the delay between frames in milliseconds." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "anim = ani.FuncAnimation(fig, animate, frames=range(1, 601), interval=100)\n", "IPython.display.display(IPython.display.HTML(anim.to_html5_video()))\n", "plt.close()" ] } ], "metadata": { "kernelspec": { "display_name": "matse", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }